package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.util.CmiSetUtil;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("EventadulterlinkDAO")
@Transactional
public class EventadulterlinkDAOImpl extends AbstractJpaDao<Eventadulterlink> implements EventadulterlinkDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Eventadulterlink.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public EventadulterlinkDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Eventadulterlink findEventadulterlinkById(Integer id) throws DataAccessException {

		return findEventadulterlinkById(id, -1, -1);
	}

	@Transactional
	public Eventadulterlink findEventadulterlinkById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventadulterlinkById", startResult, maxRows, id);
			return (Eventadulterlink) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventadulterlink> findEventadulterlinkByName(String name) throws DataAccessException {

		return findEventadulterlinkByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventadulterlink> findEventadulterlinkByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventadulterlinkByName", startResult, maxRows, name);
		return new LinkedHashSet<Eventadulterlink>(query.getResultList());
	}

	@Transactional
	public Set<Eventadulterlink> findEventadulterlinkByNameContaining(String name) throws DataAccessException {

		return findEventadulterlinkByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventadulterlink> findEventadulterlinkByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventadulterlinkByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Eventadulterlink>(query.getResultList());
	}

	@Transactional
	public Set<Eventadulterlink> findEventadulterlinkByDescription(String description) throws DataAccessException {

		return findEventadulterlinkByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventadulterlink> findEventadulterlinkByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventadulterlinkByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Eventadulterlink>(query.getResultList());
	}

	@Transactional
	public Set<Eventadulterlink> findEventadulterlinkByDescriptionContaining(String description)
			throws DataAccessException {

		return findEventadulterlinkByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventadulterlink> findEventadulterlinkByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findEventadulterlinkByDescriptionContaining", startResult, maxRows,
				description);
		return new LinkedHashSet<Eventadulterlink>(query.getResultList());
	}

	@Transactional
	public Eventadulterlink findEventadulterlinkByPrimaryKey(Integer id) throws DataAccessException {
		return findEventadulterlinkByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Eventadulterlink findEventadulterlinkByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventadulterlinkByPrimaryKey", startResult, maxRows, id);
			return (Eventadulterlink) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventadulterlink> findEventadulterlinksByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventadulterlinksByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventadulterlink> findAllEventadulterlinks() throws DataAccessException {

		return findAllEventadulterlinks(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventadulterlink> findAllEventadulterlinks(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllEventadulterlinks", startResult, maxRows);
		return new LinkedHashSet<Eventadulterlink>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Eventadulterlink> findEventadulterlinksByAdulterlink(Integer adulterlinkId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllEventadulterlinksByAdulterlink", start, max, adulterlinkId);
		return new LinkedHashSet<Eventadulterlink>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Eventadulterlink> findEventadulterlinksByEvent(Integer eventId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllEventadulterlinksByEvent", start, max, eventId);
		return new LinkedHashSet<Eventadulterlink>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventadulterlink> findEventadulterlinkByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Eventadulterlink>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Eventadulterlink> findEventadulterlinksByDetachedCriteria(CmiPagination page)
			throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Eventadulterlink.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Eventadulterlink> eventadulterlinks = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return eventadulterlinks;
	}

	@Transactional
	public Eventadulterlink findEventadulterlinkByPhysicalPrimaryKey(Integer eventId, Integer adulterlinkId)
			throws DataAccessException {
		return findEventadulterlinkByPhysicalPrimaryKey(eventId, adulterlinkId, -1, -1);
	}

	@Transactional
	public Eventadulterlink findEventadulterlinkByPhysicalPrimaryKey(Integer eventId, Integer adulterlinkId,
			int startResult, int maxRows) throws DataAccessException {
		Eventadulterlink eventadulterlink = null;
		try {
			Query query = createNamedQuery("findEventadulterlinkByPhyscialPrimaryKey", startResult, maxRows, eventId,
					adulterlinkId);
			
			List resultList = query.getResultList();
			if(resultList.size()>1) {
				eventadulterlink = (Eventadulterlink) resultList.get(0);
			}
				
			
			
			return (Eventadulterlink) eventadulterlink;
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Eventadulterlink entity) {
		return true;
	}
}
